﻿using System;
using System.Linq;
using Landpy.CruiseControlNet.FacadeInterface.ConfigurationModule;
using Landpy.CruiseControlNet.FacadeInterface.RepositoryModule;
using Landpy.CruiseControlNet.Infrastructure.Aop.WrapAttribute;

namespace Landpy.CruiseControlNet.DomainLayer.RepositoryModule
{
    public class AssemblyRepository : IAssemblyRepository
    {
        private IProjectRepository ProjectRepository { get; set; }
        private ITaskRepository TaskRepository { get; set; }

        public AssemblyRepository()
        {
            this.ProjectRepository = new ProjectRepository();
            this.TaskRepository = new TaskRepository();
        }

        [NotThrowException]
        [ExceptionMessageBox]
        [LogException]
        public Assembly GetAssemblyById(Project project, Guid nunitId, Guid id)
        {
            Assembly queryAssembly = null;
            if (project != null)
            {
                var nUnit = this.TaskRepository.GetTaskById(project, nunitId) as NUnit;
                if (nUnit != null)
                {
                    queryAssembly = (from Assembly assembly in nUnit.Assemblies
                                     where assembly.Id == id
                                     select assembly).FirstOrDefault();
                }
            }
            return queryAssembly;
        }

        [NotThrowException]
        [ExceptionMessageBox]
        [LogException]
        public Assembly GetAssemblyById(CruiseControl cruiseControl, Guid projectId, Guid nunitId, Guid id)
        {
            var queryProject = this.ProjectRepository.GetProjectById(cruiseControl, projectId);
            return this.GetAssemblyById(queryProject, nunitId, id);
        }
    }
}